﻿Imports System.IO
Imports ajayumi.develop.CSharp.Win.Controls


Public Class FrmLogin

    Private m_gbtnOffline As GlassButton = Nothing

    Protected Overrides Sub gbtnSubmit_Click(sender As Object, e As System.EventArgs)
        Me.Panel1.Visible = True
        Me.pgb.CustomText = "登陆中..."
        Me.gbtnSubmit.Enabled = False
        Me.txtUserName.ReadOnly = True
        Me.txtPassword.ReadOnly = True

        Me.BackgroundWorker1.RunWorkerAsync()
    End Sub

    Protected Overrides Sub gbtnCancel_Click(sender As Object, e As System.EventArgs)
        Me.BackgroundWorker1.CancelAsync()
        MyBase.gbtnCancel_Click(sender, e)
    End Sub

    Private Sub _FrmLogin_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.m_gbtnOffline = New GlassButton() With {.Text = "离线运行"}
        AddHandler Me.m_gbtnOffline.Click, AddressOf Me.gbtnOffline_Click
        Me.AddControlToFlowLayoutPanel(Me.m_gbtnOffline)

        If Directory.Exists(My.Settings.CacheFolder) = False Then
            Directory.CreateDirectory(My.Settings.CacheFolder)
        End If

        Me.m_gbtnOffline.Visible = False
        '判断网络是否可用，不可用则开启离线模式
        Me.Panel1.Visible = True
        Me.pgb.CustomText = "检查网络中..."
        Me.gbtnSubmit.Enabled = False
        Me.BackgroundWorker2.RunWorkerAsync()

        'Me.m_gbtnOffline.PerformClick()
    End Sub

    Private Sub FrmLogin_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Me.BackgroundWorker1.CancelAsync()
        Me.BackgroundWorker2.CancelAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim _UserName As String = Me.txtUserName.Text.Trim()
        Dim _Password As String = Me.txtPassword.Text.Trim()

        o_ReaderService = New Google.Reader.ReaderService(_UserName, _Password, o_Source)
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Me.Panel1.Visible = False
        Me.gbtnSubmit.Enabled = True
        Me.txtUserName.ReadOnly = False
        Me.txtPassword.ReadOnly = False

        If o_ReaderService.IsLogin Then
            My.Settings.UserName = Me.txtUserName.Text.Trim()
            My.Settings.Password = Me.txtPassword.Text.Trim()
            My.Settings.Save()

            o_ReaderService.CacheFolder = My.Settings.CacheFolder

            Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Else
            MsgBox("登录失败，请检查用户名、密码是否正确。")
            Me.m_gbtnOffline.Visible = True
        End If
    End Sub

    Private Sub BackgroundWorker2_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
        Dim _IsAvailable As Boolean = My.Computer.Network.IsAvailable
        Dim _IsConnection As Boolean = False
        If _IsAvailable = True Then
            Try
                _IsConnection = My.Computer.Network.Ping("www.google.com")
            Catch ex As Exception

            End Try

            If _IsConnection = True Then
                o_ReaderMode = ReaderMode.Online
            Else
                o_ReaderMode = ReaderMode.Offline
            End If
        Else
            o_ReaderMode = ReaderMode.Offline
        End If
    End Sub

    Private Sub BackgroundWorker2_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
        Me.Panel1.Visible = False
        Me.gbtnSubmit.Enabled = True

        Select Case o_ReaderMode
            Case ReaderMode.Online
                Dim _UserName As String = My.Settings.UserName
                Dim _Password As String = My.Settings.Password
                If String.IsNullOrEmpty(_UserName) = False AndAlso String.IsNullOrEmpty(_Password) = False Then
                    Me.txtUserName.Text = _UserName
                    Me.txtPassword.Text = _Password

                    Me.gbtnSubmit.PerformClick()
                End If
            Case ReaderMode.Offline
                Me.doOfflineMode()
        End Select
    End Sub


    Private Sub gbtnOffline_Click(sender As System.Object, e As System.EventArgs)
        o_ReaderMode = ReaderMode.Offline
        Me.doOfflineMode()
    End Sub

    Private Sub doOfflineMode()
        o_ReaderService = New Localhost.ReaderService(My.Settings.CacheFolder)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub

End Class